package com.prs.crm.action.project;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.oa.OaTaskSource;
import com.prs.crm.domain.project.ProjectSource;
import com.prs.crm.domain.project.ProjectType;
import com.prs.crm.service.project.ProjectTypeService;
import com.prs.crm.service.sys.LogService;

public class ProjectTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ProjectTypeService projectTypeService;

	private PaginationSupport pagination;

	private ProjectType type;
	
	private String typeName;
    
	private Integer[] typeIds;
	
	private LogService logService;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public void setProjectTypeService(ProjectTypeService projectTypeService) {
		this.projectTypeService = projectTypeService;
	}

	public ProjectTypeService getProjectTypeService() {
		return projectTypeService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public ProjectType getType() {
		return type;
	}

	public void setType(ProjectType type) {
		this.type = type;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	@Action("listType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProjectType.class);
		this.setPagination(this.getProjectTypeService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editType")
	public String edit() {

		return SUCCESS;
	}

	@Action(value = "saveType", results = {
			@Result(name = "success", type = "redirect", location = "listType"),
			@Result(name = "error", type = "chain", location = "editType") })
	public String save() {
		if (type.getName() == null || "".equals(type.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(ProjectType.class,type.getName())) {
			if(!(type.getName().equals(typeName))){
			   this.addActionError("名称已经存在");
			   return ERROR;
			}
		}
		this.getProjectTypeService().save(type);
		return SUCCESS;
	}
	@Action(value = "removeType", results = {
			@Result(name = "success", type = "redirect", location = "listType"),
			@Result(name = "error", type = "chain", location = "listType") })
	public String remove() {
		if (typeIds == null) {
			this.addActionError("请选择要删除的类型");
			return ERROR;
		}
		for (Integer id : typeIds) {
			ProjectType type = this.getProjectTypeService().get(id);
			if (type.getProjects().size() != 0) {
				this.addActionError("已有任务引用，不能删除");
				return ERROR;
			}
		}
		this.getProjectTypeService().batchRemove(typeIds);
		return SUCCESS;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
}
